package br.com.scitv.tela;

import java.awt.event.KeyEvent;

import com.sun.dtv.resources.ScarceResource;
import com.sun.dtv.resources.ScarceResourceListener;
import com.sun.dtv.ui.Capabilities;
import com.sun.dtv.ui.DTVContainer;
import com.sun.dtv.ui.Device;
import com.sun.dtv.ui.Plane;
import com.sun.dtv.ui.PlaneSetup;
import com.sun.dtv.ui.Screen;
import com.sun.dtv.ui.event.RemoteControlEvent;
import com.sun.dtv.ui.event.UserInputEvent;
import com.sun.dtv.ui.event.UserInputEventListener;
import com.sun.dtv.ui.event.UserInputEventManager;

public abstract class Tela implements UserInputEventListener, ScarceResourceListener 
{
	private Plane plane;
	protected PlaneSetup planeSetup;
	protected DTVContainer dtvcontainer;
	protected int borda;

	
	public Tela() {
		borda = 10;
	}
	


	public void iniciar() {
		
		Device device = Device.getInstance();
		Screen currentScreen = device.getDefaultScreen();
		UserInputEventManager manager = UserInputEventManager.getUserInputEventManager(currentScreen);
		RemoteControlEvent anyColoredKeyTyped = 
			new RemoteControlEvent(null, java.awt.event.KeyEvent.KEY_TYPED, 0, 0, RemoteControlEvent.VK_COLORED, KeyEvent.CHAR_UNDEFINED);

		try {
			anyColoredKeyTyped.reserve(true, -1, null);
		} catch (Exception e) {
			e.printStackTrace();
		}

		manager.removeAllUserInputEventListeners();
		manager.addUserInputEventListener(this, anyColoredKeyTyped);

		Plane[] planes = currentScreen.getAllPlanes();

		for(int i=0; i<planes.length; i++) {
			Capabilities cap = planes[i].getCapabilities();
			if (cap.isGraphicsRenderingSupported()) {
				plane = planes[i];
				planeSetup = plane.getCurrentSetup();
				break;
			}
		}

		try {
			plane.reserve(false,-1,this);
		} catch (Exception e) {
			e.printStackTrace();
		}

		dtvcontainer = DTVContainer.getDTVContainer(plane);
//		dtvcontainer.removeAll();
		dtvcontainer.setSize(planeSetup.getScreenResolution());
		dtvcontainer.setVisible(true);
		
//		addComponentes();		
	}
	
	
	public abstract void addComponentes() throws Exception;


	public abstract void userInputEventReceived(UserInputEvent inputEvent);


	public void releaseForced(ScarceResource arg0) {
		arg0.release();
	}


	public boolean releaseRequested(ScarceResource arg0) {
		return false;
	}


	public void released(ScarceResource arg0) {
		arg0.release();
	}



	public DTVContainer getDtvcontainer() {
		return dtvcontainer;
	}



	public void setDtvcontainer(DTVContainer dtvcontainer) {
		this.dtvcontainer = dtvcontainer;
	}
}
